<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tootip</title>
  <style>
    .container {
      max-width: 800px;
      margin: 30px auto;
      border-radius: 4px;
      box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.3);
      padding: 16px;
    }
    
    .button {
      padding: 10px 16px;
      font-size: 14px;
      font-weight: 500;
      color: #303030;
      border: 1px solid #ccc;
      border-radius: 4px;
      outline: none;
      cursor: pointer;
      position: relative;
    }

    .button:hover {
      border-color: lightskyblue;
      color: lightskyblue;
    }

    .tootip {
      position: absolute;
      z-index: 10;
      background: #000;
      color: #fff;
      display: none;
      white-space: nowrap;
      padding: 3px 6px;
      border-radius: 4px;
    }

    .tootip::before {
      content: '';
      width: 0;
      height: 0;
      position: absolute;
      border: 6px solid transparent;

    }

    .tootip.top {
      bottom: calc(100% + 10px);
      left: 50%;
      transform: translateX(-50%);
    }

    .tootip.top::before {
      border-top-color: #303030;
      transform: translateX(-50%);
      top: 100%;
      left: 50%;
    }

    .tootip.bottom {
      top: calc(100% + 10px);
      left: 50%;
      transform: translateX(-50%);
    }

  .tootip.bottom::before {
    border-bottom-color: #303030;
    transform: translateX(-50%);
    bottom: 100%;
    left: 50%;
  }

  .tootip.left {
    right: calc(100% + 10px);
    top: 50%;
    transform: translateY(-50%);
  }

  .tootip.left::before {
    border-left-color: #303030;
    transform: translateY(-50%);
    top: 50%;
    right: -11px;
  }

  .tootip.right {
    left: calc(100% + 10px);
    top: 50%;
    transform: translateY(-50%);
  }

  .tootip.right::before {
    border-right-color: #303030;
    transform: translateY(-50%);
    top: 50%;
    left: -11px;
  }

    .tootip.show {
      display: block;
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>Tootip</h2>
    <div class="btn-group">
      <button class="button" data-name='tootip' data-align='top' data-text="我是内容">上</button>
      <button class="button" data-name='tootip' data-align='bottom' data-text="我是内容">下</button>
      <button class="button" data-name='tootip' data-align='left' data-text="我是内容">左</button>
      <button class="button" data-name='tootip' data-align='right' data-text="我是内容">右</button>
    </div>
  </div>

  <script>
    class Tootip {
      constructor($root) {
        this.$root = $root
        this.$root.tootip = this
        this.align = $root.dataset.align
        this.text = $root.dataset.text

        this.render()
        this.setRoot()
        this.show()
      }
      render() {
        let $tootip = document.createElement('div')
        $tootip.classList.add('tootip')
        $tootip.classList.add(this.align)
        $tootip.innerText = this.text
        this.$tootip = $tootip
        this.$root.appendChild($tootip)
      }

      setRoot() {
        this.$root.showTootip = this.show.bind(this)
        this.$root.hideTootip = this.hide.bind(this)
      }

      show() {
        this.$tootip.classList.add('show')
      }

      hide() {
        this.$tootip.classList.remove('show')
      }
    }

    document.querySelectorAll('[data-name="tootip"]').forEach($node => {
      $node.onmouseenter = function() {
        if($node.tootip) {
          $node.showTootip()
        } else {
          new Tootip(this)
        }
      }
      $node.onmouseleave = function() {
        $node.hideTootip()
      }
    })
  </script>
</body>
</html>